using System.ComponentModel;
using Guadagno.Utilities;

namespace TaskService.CustomInstallActions
{
    /// <summary>
    /// The service installation class.
    /// </summary>
    /// <remarks>
    /// This class is responsible for installing and configuring the Windows service.
    /// </remarks>
    [RunInstaller(true)]
    public class ServiceInstall : System.Configuration.Install.Installer
    {

        #region Private Variables
        
        private System.ServiceProcess.ServiceProcessInstaller _serviceProcessInstaller;
        private ServiceInstallerEx _serviceInstaller;

        #endregion Private Variables

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceInstall"/> class.
        /// </summary>
        public ServiceInstall()
        {
            InitializeComponent();
            
           // For Debugging the Custom Install Actions
            //System.Diagnostics.Debugger.Launch();
            
            // Add the CustomFolderProperties installer class
            CustomFolderProperties cfp = new CustomFolderProperties();
            Installers.Add(cfp);
            
            // Add the CreateShorts installer class
            CreateShortcuts cs = new CreateShortcuts();
            Installers.Add(cs);

            // Set the recover action to restart the service after 60 seconds, only the first time
            _serviceInstaller.FailureActions.Add(new FailureAction(RecoverAction.Restart, 60000));
            _serviceInstaller.FailureActions.Add(new FailureAction(RecoverAction.None, 0));
            _serviceInstaller.FailureActions.Add(new FailureAction(RecoverAction.None, 0));

            // Configure the service to start right after it is installed.
            _serviceInstaller.StartOnInstall = true;
        }

        #endregion Constructors

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this._serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this._serviceInstaller = new ServiceInstallerEx();
            // 
            // _serviceProcessInstaller
            // 
            this._serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this._serviceProcessInstaller.Password = null;
            this._serviceProcessInstaller.Username = null;
            // 
            // _serviceInstaller
            // 
            this._serviceInstaller.DisplayName = Properties.Resources.Service_DisplayName;
            this._serviceInstaller.ServiceName = Properties.Resources.Service_ServiceName;
            this._serviceInstaller.Description = Properties.Resources.Service_Description;
            this._serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            // 
            // ServiceInstall
            // 
            this.Installers.Add(this._serviceInstaller);
            this.Installers.Add(this._serviceProcessInstaller);
        }
        #endregion

    }
}
